package com.vidmt.api.modules.sys.service;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.Menu;
import com.vidmt.api.common.utils.SpringUtil;
import com.vidmt.api.common.utils.StrUtil;
import com.vidmt.api.common.utils.XmlD4jUtil;

@Service
@Transactional(readOnly = false)
public class MenuService extends BaseService {
	public static final String CACHE_MENU_LIST = "menuList";

	private List<Menu> menulist;

	public static void main(String[] args) throws IOException {
		MenuService svc = new MenuService();
		ResourceLoader resourceLoader = new DefaultResourceLoader();
		Resource resource = resourceLoader.getResource("menu.xml");
		List<Menu> menuList = svc.loadMenu(resource.getFile());
		System.out.println(Arrays.toString(menuList.toArray()));
	}

	private String[] getPermissions(String url) {
		Method method = SpringUtil.getHandler(url);
		if (method != null) {
			RequiresPermissions permanno = method.getAnnotation(RequiresPermissions.class);
			if (permanno != null) {
				String[] perms = permanno.value();
				return perms;
			}
		}
		return null;
	}

	public Menu load(String id) {
		for (Menu menu : menulist) {
			if (id.equals(menu.getId())) {
				return menu;
			}
		}
		return null;
		// return dao.get(id);
	}

	public List<Menu> findMenuList() {
		if (menulist == null) {
			ResourceLoader resourceLoader = new DefaultResourceLoader();
			Resource resource = resourceLoader.getResource("menu.xml");
			try {
				menulist = loadMenu(resource.getFile());
				for (Menu menu : menulist) {
					if(menu.getHref()!=null){
						String[] perms = getPermissions(menu.getHref());
						if (perms != null) {
							menu.setPermission(StrUtil.join(perms));
						}
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return menulist;
		// return dao.findAllList(new Menu());
	}

	private List<Menu> loadMenu(File file) {
		List<Menu> menulist = Lists.newArrayList();

		Document doc = XmlD4jUtil.getXmlDoc(file);
		Element root = doc.getRootElement();
		recurseMenu(menulist, null, root, 0);
		return menulist;
	}

	private void recurseMenu(List<Menu> allmenulist, Menu parent, Element el, int sibidx) {
		String parentid = "";
		String parentids = "0";
		if (parent != null) {
			parentid = parent.getId();
			parentids = parent.getParentIds() + "," + parentid;
		}
		Menu menu = new Menu();
		menu.setId(parentid + (sibidx + 1));
		menu.setIcon(el.attributeValue("icon"));
		menu.setHref(el.attributeValue("href"));
		menu.setName(el.attributeValue("name"));
		menu.setTarget(el.attributeValue("target"));
		menu.setParent(parent);
		menu.setParentIds(parentids);
		List<Element> elements = el.elements();
		if (elements != null && elements.size() > 0) {
			for (int i = 0; i < elements.size(); i++) {
				recurseMenu(allmenulist, menu, elements.get(i), i);
			}
		}
		allmenulist.add(menu);
	}
}
